<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>初步爬虫小项目 | 爬虫</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../正则表达式/正则表达式.html" />
    
    
    <link rel="prev" href="../认识爬虫/Requests.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="1.3"
        data-chapter-title="初步爬虫小项目"
        data-filepath="认识爬虫/初步爬虫.md"
        data-basepath=".."
        data-revision="Fri Oct 19 2018 08:44:08 GMT+0800 (中国标准时间)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        序言
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="认识爬虫/introduceSpider.html">
            
                
                    <a href="../认识爬虫/introduceSpider.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        认识爬虫
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="认识爬虫/http.html">
            
                
                    <a href="../认识爬虫/http.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        HTTP
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="认识爬虫/Requests.html">
            
                
                    <a href="../认识爬虫/Requests.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        requests
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="1.3" data-path="认识爬虫/初步爬虫.html">
            
                
                    <a href="../认识爬虫/初步爬虫.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        初步爬虫小项目
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="正则表达式/正则表达式.html">
            
                
                    <a href="../正则表达式/正则表达式.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        正则表达式
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="正则表达式/正则表达式练习.html">
            
                
                    <a href="../正则表达式/正则表达式练习.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        正则表达式练习
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="正则表达式/正则表达式之后的第一个项目.html">
            
                
                    <a href="../正则表达式/正则表达式之后的第一个项目.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        正则表达式之后的第一个项目
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="正则表达式/正则表达式常用表.html">
            
                
                    <a href="../正则表达式/正则表达式常用表.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        正则表达式常用表
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="正则表达式/正则表达式重点1.html">
            
                
                    <a href="../正则表达式/正则表达式重点1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        正则表达式重点
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="正则表达式/正则表达式常问问题.html">
            
                
                    <a href="../正则表达式/正则表达式常问问题.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        正则表达式常问问题
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="正则表达式/正则爬虫练习.html">
            
                
                    <a href="../正则表达式/正则爬虫练习.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.6.</b>
                        
                        正则爬虫项目练习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="XPATH/xpath-in.html">
            
                
                    <a href="../XPATH/xpath-in.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        XPATH
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="XPATH/Xpath.html">
            
                
                    <a href="../XPATH/Xpath.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        XPATH认识
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="XPATH/Xpath的代码例子.html">
            
                
                    <a href="../XPATH/Xpath的代码例子.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        XPATH的代码例子
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="XPATH/xpath的爬虫练习.html">
            
                
                    <a href="../XPATH/xpath的爬虫练习.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        XPATH的爬虫练习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="BeautifulSoup/BeautifulSoup.html">
            
                
                    <a href="../BeautifulSoup/BeautifulSoup.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        BeautifulSoup
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="BeautifulSoup/常用的css选择器.html">
            
                
                    <a href="../BeautifulSoup/常用的css选择器.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        常用的css选择器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="BeautifulSoup/BeautifulSoup4的各种例子.html">
            
                
                    <a href="../BeautifulSoup/BeautifulSoup4的各种例子.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        BeautifulSoup4的各种例子
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="代理/proxy.html">
            
                
                    <a href="../代理/proxy.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        ip代理池项目
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6" data-path="Selenium/Selenium.html">
            
                
                    <a href="../Selenium/Selenium.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        Selenium
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7" data-path="进程线程协程/introduce.html">
            
                
                    <a href="../进程线程协程/introduce.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        进程线程协程
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1" data-path="进程线程协程/gevent.html">
            
                
                    <a href="../进程线程协程/gevent.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.</b>
                        
                        gevent
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.2" data-path="进程线程协程/green_let.html">
            
                
                    <a href="../进程线程协程/green_let.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.2.</b>
                        
                        green_let
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.3" data-path="进程线程协程/yield.html">
            
                
                    <a href="../进程线程协程/yield.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.3.</b>
                        
                        yield
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.4" data-path="进程线程协程/multiprecessing.html">
            
                
                    <a href="../进程线程协程/multiprecessing.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.4.</b>
                        
                        multiprecessing
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.5" data-path="进程线程协程/threading.html">
            
                
                    <a href="../进程线程协程/threading.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.5.</b>
                        
                        threading
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="8" data-path="scrapy框架/scrapy.html">
            
                
                    <a href="../scrapy框架/scrapy.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        Scrapy
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.1" data-path="scrapy框架/scrapy_setting.html">
            
                
                    <a href="../scrapy框架/scrapy_setting.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.1.</b>
                        
                        scrapy_setting
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.2" data-path="scrapy框架/模块作用.html">
            
                
                    <a href="../scrapy框架/模块作用.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.2.</b>
                        
                        模块作用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.3" data-path="scrapy框架/19个中间件.html">
            
                
                    <a href="../scrapy框架/19个中间件.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.3.</b>
                        
                        19个中间件
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="9" data-path="scrapy-redis分布式/scrapy-redis.html">
            
                
                    <a href="../scrapy-redis分布式/scrapy-redis.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.</b>
                        
                        scrapy-redis分布式
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="9.1" data-path="scrapy-redis分布式/scrapy-redis的改造方法.html">
            
                
                    <a href="../scrapy-redis分布式/scrapy-redis的改造方法.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.1.</b>
                        
                        scrapy-redis的改造方法
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="10" data-path="spider_projects/introduce.html">
            
                
                    <a href="../spider_projects/introduce.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.</b>
                        
                        spider_projects
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="10.1" data-path="spider_projects/使用代理在普通爬虫脚本下.html">
            
                
                    <a href="../spider_projects/使用代理在普通爬虫脚本下.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.1.</b>
                        
                        使用代理在普通爬虫脚本下
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.2" data-path="spider_projects/xpath爬取前程无忧的数据.html">
            
                
                    <a href="../spider_projects/xpath爬取前程无忧的数据.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.2.</b>
                        
                        xpath爬取前程无忧的数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.3" data-path="spider_projects/存储数据库的脚本.html">
            
                
                    <a href="../spider_projects/存储数据库的脚本.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.3.</b>
                        
                        存储数据库的脚本
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.4" data-path="spider_projects/weibo.html">
            
                
                    <a href="../spider_projects/weibo.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.4.</b>
                        
                        爬取央视新闻微博
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.5" data-path="spider_projects/weibo2.html">
            
                
                    <a href="../spider_projects/weibo2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.5.</b>
                        
                        爬取明星的微博
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.6" data-path="spider_projects/liepin.html">
            
                
                    <a href="../spider_projects/liepin.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.6.</b>
                        
                        爬取猎聘的职位信息Crawl
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.7" data-path="spider_projects/zhilianRe.html">
            
                
                    <a href="../spider_projects/zhilianRe.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.7.</b>
                        
                        纯正则爬取智联的职位信息
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.8" data-path="spider_projects/setting.html">
            
                
                    <a href="../spider_projects/setting.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.8.</b>
                        
                        scrapy中的setting
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.9" data-path="spider_projects/items.html">
            
                
                    <a href="../spider_projects/items.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.9.</b>
                        
                        scrapy中的items
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.10" data-path="spider_projects/pipeline.html">
            
                
                    <a href="../spider_projects/pipeline.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.10.</b>
                        
                        scrapy中的pipeline
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.11" data-path="spider_projects/middlewares.html">
            
                
                    <a href="../spider_projects/middlewares.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.11.</b>
                        
                        scrapy中的middlewares
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.12" data-path="spider_projects/框架中的代码运行脚本.html">
            
                
                    <a href="../spider_projects/框架中的代码运行脚本.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.12.</b>
                        
                        框架中的代码运行脚本
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="11" data-path="爬虫的面试题/面试题1-10.html">
            
                
                    <a href="../爬虫的面试题/面试题1-10.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>11.</b>
                        
                        爬虫面试题
                    </a>
            
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >爬虫</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="&#x521D;&#x6B65;&#x722C;&#x866B;">&#x521D;&#x6B65;&#x722C;&#x866B;</h1>
<hr>
<h3 id="&#x722C;&#x53D6;python&#x5B98;&#x7F51;&#x9996;&#x9875;">&#x722C;&#x53D6;python&#x5B98;&#x7F51;&#x9996;&#x9875;</h3>
<pre><code class="lang-python">
<span class="hljs-keyword">import</span> requests

url = <span class="hljs-string">&apos;https://www.python.org/&apos;</span>
response = requests.get(url)
<span class="hljs-keyword">with</span> open(<span class="hljs-string">&apos;python.html&apos;</span>,<span class="hljs-string">&apos;wb&apos;</span>) <span class="hljs-keyword">as</span> f:
    f.write(response.content)
</code></pre>
<hr>
<h3 id="&#x767E;&#x5EA6;&#x9996;&#x9875;&#x7684;&#x722C;&#x53D6;">&#x767E;&#x5EA6;&#x9996;&#x9875;&#x7684;&#x722C;&#x53D6;</h3>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> requests
<span class="hljs-keyword">import</span> json
<span class="hljs-comment">#&#x9700;&#x8981;&#x6709;&#x8F93;&#x5165; &#xFF0C; url</span>
url = <span class="hljs-string">&apos;http://www.baidu.com&apos;</span>
<span class="hljs-comment">#&#x51FD;&#x6570;&#x7684;&#x4E3B;&#x4F53;</span>
response = requests.get(url)
<span class="hljs-comment">#&#x6253;&#x5370;&#x8F93;&#x51FA;&#xFF0C;text &#x5C31;&#x662F;response &#x5185;&#x5B58;&#x50A8;&#x7684;&#x6570;&#x636E;&#x7684;&#x5185;&#x5BB9;</span>
<span class="hljs-comment"># print(type(response.text))</span>
<span class="hljs-comment"># print(type(response.content))</span>

<span class="hljs-comment">#&#x76F4;&#x63A5;&#x505A;&#x5B58;&#x50A8;</span>
<span class="hljs-keyword">with</span> open(<span class="hljs-string">&apos;baidu.html&apos;</span>,<span class="hljs-string">&apos;wb&apos;</span>) <span class="hljs-keyword">as</span> f:
    f.write(response.content)
</code></pre>
<hr>
<h3 id="&#x767E;&#x5EA6;&#x7FFB;&#x8BD1;&#x7684;&#x722C;&#x53D6;">&#x767E;&#x5EA6;&#x7FFB;&#x8BD1;&#x7684;&#x722C;&#x53D6;</h3>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> requests

url = <span class="hljs-string">&quot;https://fanyi.baidu.com/sug&quot;</span>

<span class="hljs-comment">#&#x751F;&#x6210;&#x4E00;&#x4E2A; form &#x7684;&#x5B57;&#x5178; &#x5FC5;&#x987B;&#x662F;&#x5B57;&#x5178;</span>
form = {
    <span class="hljs-string">&apos;kw&apos;</span>:<span class="hljs-string">&apos;&#x7C89;&apos;</span>
}
<span class="hljs-comment">#&#x722C;&#x866B;&#x7B2C;&#x4E09;&#x6B65;</span>

response = requests.post(url,data=form)
<span class="hljs-comment">#json()&#x51FD;&#x6570;&#x5C31;&#x662F;&#x8FD4;&#x56DE;&#x7ED9;&#x6211;&#x4EEC;python&#x5BF9;&#x8C61;&#x7684;&#x7C7B;</span>
<span class="hljs-comment">#equals res_dict = json.loads(response.text)</span>
res_dict = response.json()
<span class="hljs-comment">#&#x83B7;&#x53D6;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x7684;&#x4FE1;&#x606F; &#x722C;&#x866B;&#x4E2D;&#x7684;&#x7B2C;&#x56DB;&#x6B65;</span>
translated = res_dict[<span class="hljs-string">&apos;data&apos;</span>][<span class="hljs-number">0</span>][<span class="hljs-string">&apos;v&apos;</span>]

print(<span class="hljs-string">&apos;&#x7C89;&#x7684;&#x7FFB;&#x8BD1;&#x5C31;&#x662F;&#xFF1A;&apos;</span>+ translated)
<span class="hljs-comment"># print(response.text)</span>
</code></pre>
<hr>
<h2 id="requests-&#x5E93;&#x7684;&#x8FDB;&#x9636;&#x7528;&#x6CD5;">Requests &#x5E93;&#x7684;&#x8FDB;&#x9636;&#x7528;&#x6CD5;</h2>
<h3 id="&#x4EBA;&#x4EBA;&#x7F51;&#x7684;&#x521D;&#x6B65;&#x722C;&#x53D6;">&#x4EBA;&#x4EBA;&#x7F51;&#x7684;&#x521D;&#x6B65;&#x722C;&#x53D6;</h3>
<h4 id="&#x4EBA;&#x4EBA;&#x7F51;&#x767B;&#x9646;&#x9700;&#x8981;cookie&#x65F6;">&#x4EBA;&#x4EBA;&#x7F51;&#x767B;&#x9646;&#x9700;&#x8981;cookie&#x65F6;</h4>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> requests

url = <span class="hljs-string">&apos;http://www.renren.com/967976797/profile&apos;</span>

<span class="hljs-comment">#&#x5F53;&#x9700;&#x8981;&#x767B;&#x5F55;&#x6BD4;&#x8F83;&#x9EBB;&#x70E6;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x5DF2;&#x7ECF;&#x767B;&#x5F55;&#x8FC7;&#x7684;&#x5185;&#x5BB9;&#x7684;cookie&#x503C;&#x8FDB;&#x884C;&#x8BBF;&#x95EE;&#xFF0C;&#x767B;&#x9646;&#x540E;&#x624D;&#x80FD;&#x8BBF;&#x95EE;&#x7684;&#x9875;&#x9762;</span>
headers = {
    <span class="hljs-string">&apos;Accept&apos;</span>: <span class="hljs-string">&apos;text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8&apos;</span>,
    <span class="hljs-comment"># &apos;Accept-Encoding&apos;: &apos;gzip, deflate, sdch&apos;,</span>
    <span class="hljs-string">&apos;Accept-Language&apos;</span>: <span class="hljs-string">&apos;zh-CN,zh;q=0.8&apos;</span>,
    <span class="hljs-string">&apos;Cache-Control&apos;</span>: <span class="hljs-string">&apos;max-age=0&apos;</span>,
    <span class="hljs-string">&apos;Connection&apos;</span>: <span class="hljs-string">&apos;keep-alive&apos;</span>,
    <span class="hljs-string">&apos;Cookie&apos;</span>: <span class="hljs-string">&apos;anonymid=jlxl0mggdo5rqv; depovince=GW; _r01_=1; jebe_key=7f2d6822-37f7-4ab7-b367-e13c8670fc12%7C17348ebab6f4bb4ad4fa1da95d483e7d%7C1536663196307%7C1%7C1536663415354; jebe_key=7f2d6822-37f7-4ab7-b367-e13c8670fc12%7C17348ebab6f4bb4ad4fa1da95d483e7d%7C1536663196307%7C1%7C1536663418560; JSESSIONID=abcicUQJGC0bZ6OexDixw; __utmt=1; __utma=151146938.2068577366.1536669084.1536669084.1536669084.1; __utmb=151146938.1.10.1536669084; __utmc=151146938; __utmz=151146938.1536669084.1.1.utmcsr=renren.com|utmccn=(referral)|utmcmd=referral|utmcct=/967976797/profile; vip=1; jebecookies=ea86b031-6370-4ee7-84eb-91e9c574ef1f|||||; ick_login=21b202fd-23b1-405c-9c9d-9013aecb58b6; _de=5F464D920D5EAF281D007C7E767C48C1; p=8526c37f72970154096ae1f748ed16027; first_login_flag=1; ln_uact=18665945450; ln_hurl=http://head.xiaonei.com/photos/0/0/men_main.gif; t=b724f429d33707415176a6ad2ca6b8427; societyguester=b724f429d33707415176a6ad2ca6b8427; id=967976797; xnsid=375cf4b7; loginfrom=syshome; wp_fold=0&apos;</span>,
    <span class="hljs-string">&apos;Host&apos;</span>: <span class="hljs-string">&apos;www.renren.com&apos;</span>,
    <span class="hljs-string">&apos;Referer&apos;</span>: <span class="hljs-string">&apos;http://zhibo.renren.com/top&apos;</span>,
    <span class="hljs-string">&apos;Upgrade-Insecure-Requests&apos;</span>: <span class="hljs-string">&apos;1&apos;</span>,
    <span class="hljs-string">&apos;User-Agent&apos;</span>: <span class="hljs-string">&apos;Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36&apos;</span>,
}

response = requests.get(url, headers=headers)

<span class="hljs-keyword">with</span> open(<span class="hljs-string">&apos;renren6.html&apos;</span>, <span class="hljs-string">&apos;wb&apos;</span>) <span class="hljs-keyword">as</span> f:
    f.write(response.content)
</code></pre>
<p><strong>&#x7B2C;&#x4E8C;&#x79CD;&#x65B9;&#x5F0F;</strong></p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> requests

<span class="hljs-comment">#&#x56E0;&#x4E3A;&#x9700;&#x8981;&#x4FDD;&#x5B58;cookie&#xFF0C;&#x6240;&#x4EE5;&#x6211;&#x4EEC;&#x9700;&#x8981;requests&#x4E2D;&#x4FDD;&#x5B58;cookie&#x7684;&#x7C7B;&#xFF0C;&#x8FD9;&#x4E2A;&#x7C7B;&#x53EB;&#x505A;session</span>
s = requests.session()

url = <span class="hljs-string">&apos;http://www.renren.com/PLogin.do&apos;</span>

form = {
    <span class="hljs-string">&apos;email&apos;</span>: <span class="hljs-string">&apos;18665945450&apos;</span>,
    <span class="hljs-string">&apos;password&apos;</span>: <span class="hljs-string">&apos;802323&apos;</span>
}

<span class="hljs-comment">#&#x7528;s(session)&#x66FF;&#x4EE3;requests,&#x5C31;&#x80FD;&#x591F;&#x4FDD;&#x5B58;cookie&#x5230;session&#x4E2D;</span>
response = s.post(url,data=form)

<span class="hljs-keyword">with</span> open(<span class="hljs-string">&apos;renren.html&apos;</span>,<span class="hljs-string">&apos;wb&apos;</span>) <span class="hljs-keyword">as</span> f:
    f.write(response.content)

url_private = <span class="hljs-string">&apos;http://www.renren.com/privacyhome.do&apos;</span>

response = s.get(url_private)

<span class="hljs-keyword">with</span> open(<span class="hljs-string">&apos;private.html&apos;</span>, <span class="hljs-string">&apos;wb&apos;</span>) <span class="hljs-keyword">as</span> f:
    f.write(response.content)
</code></pre>
<p><strong>&#x767B;&#x9646;&#x4EBA;&#x4EBA;&#x7F51;&#x65F6;&#xFF0C;&#x4F1A;&#x51FA;&#x73B0;&#x4E00;&#x4E2A;&#x767B;&#x9646;&#x9875;&#x9762;&#xFF0C;&#x767B;&#x9646;&#x9875;&#x9762;&#x4F1A;&#x89E6;&#x53D1;&#x4E00;&#x4E2A;Ajax&#xFF0C;&#x767B;&#x9646;&#x9875;&#x9762;&#x53C8;&#x662F;&#x4E00;&#x4E2A;form&#x8868;&#x5355;&#x7684;&#x63D0;&#x4EA4;&#x9875;&#x9762;&#xFF0C;&#x6240;&#x4EE5;&#x8BF4;&#x8FD9;&#x4E2A;&#x65F6;&#x5019;&#x5C31;&#x9700;&#x8981;&#x53E6;&#x4E00;&#x79CD;&#x65B9;&#x5F0F;&#x6765;&#x722C;&#x53D6;&#x4FE1;&#x606F;&#x3002;</strong></p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> requests

s = requests.session()

url = <span class="hljs-string">&apos;http://www.renren.com/ajaxLogin/login?1=1&amp;uniqueTimestamp=2018821911387&apos;</span>

form = {
    <span class="hljs-string">&apos;email&apos;</span>: <span class="hljs-string">&apos;18665945450&apos;</span>,
    <span class="hljs-string">&apos;icode&apos;</span>: <span class="hljs-string">&apos;&apos;</span>,
    <span class="hljs-string">&apos;origURL:http&apos;</span>: <span class="hljs-string">&apos;http://www.renren.com/home&apos;</span>,
    <span class="hljs-string">&apos;domain&apos;</span>: <span class="hljs-string">&apos;renren.com&apos;</span>,
    <span class="hljs-string">&apos;key_id&apos;</span>: <span class="hljs-string">&apos;1&apos;</span>,
    <span class="hljs-string">&apos;captcha_type&apos;</span>: <span class="hljs-string">&apos;web_login&apos;</span>,
    <span class="hljs-string">&apos;password&apos;</span>: <span class="hljs-string">&apos;6456327ee115979e4bd8e765ec5833711be3dd1599a11d1166a2a10aa63d4b8b&apos;</span>,
    <span class="hljs-string">&apos;rkey&apos;</span>: <span class="hljs-string">&apos;bf2984d600797ab045171eb14aa24b9f&apos;</span>,
    <span class="hljs-string">&apos;f&apos;</span>: <span class="hljs-string">&apos;http%3A%2F%2Fzhibo.renren.com%2Ftop&apos;</span>,
}

response = s.post(url,data=form)
home_url = <span class="hljs-string">&apos;http://www.renren.com/Home.do&apos;</span>
response = s.get(home_url)
<span class="hljs-keyword">with</span> open(<span class="hljs-string">&apos;home.html&apos;</span>, <span class="hljs-string">&apos;wb&apos;</span>) <span class="hljs-keyword">as</span> f:
    f.write(response.content)


<span class="hljs-comment"># print(response.text)</span>
<span class="hljs-comment">#{&quot;code&quot;:true,&quot;homeUrl&quot;:&quot;http://www.renren.com/Home.do&quot;}</span>
</code></pre>
<hr>
<h2 id="&#x56FE;&#x7247;&#x7684;&#x722C;&#x53D6;">&#x56FE;&#x7247;&#x7684;&#x722C;&#x53D6;</h2>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> requests

<span class="hljs-keyword">import</span> json

<span class="hljs-keyword">import</span> os
url = <span class="hljs-string">&apos;https://www.toutiao.com/search_content/?offset=60&amp;format=json&amp;keyword=%E5%B8%85%E5%93%A5%E5%9B%BE%E7%89%87&amp;autoload=true&amp;count=20&amp;cur_tab=1&amp;from=search_tab&apos;</span>

response = requests.get(url)
<span class="hljs-comment"># &#x4E3B;&#x8981;&#x6709;&#x4E00;&#x4E2A;&#x6587;&#x4EF6;&#x5939;&#x6765;&#x5B58;&#x50A8;&#x56FE;&#x7247;</span>
dictionary_image = <span class="hljs-string">&apos;download&apos;</span>
<span class="hljs-comment"># &#x5224;&#x65AD;&#x662F;&#x5426;&#x5B58;&#x5728;download&#x6587;&#x4EF6;&#x5939;</span>
<span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> os.path.exists(dictionary_image):
    <span class="hljs-comment"># &#x5982;&#x679C;&#x4E0D;&#x5B58;&#x5728;&#x5C31;&#x521B;&#x5EFA;&#x8FD9;&#x4E2A;&#x6587;&#x4EF6;&#x5939;</span>
    os.mkdir(dictionary_image)

<span class="hljs-comment"># json&#x4E2D;&#x7684;loads&#x51FD;&#x6570;&#x662F;&#xFF0C;&#x4ECE;&#x5B57;&#x7B26;&#x4E32;&#x8F6C;&#x6362;&#x6210;json&#x7684;&#x7C7B;&#x578B;&#xFF0C;&#x53EF;&#x80FD;&#x662F;&#x5B57;&#x5178;&#xFF0C;&#x4E5F;&#x53EF;&#x80FD;&#x662F;&#x5217;&#x8868;</span>
<span class="hljs-comment"># &#x4F20;&#x5165;&#x7684;&#x4FE1;&#x606F;&#x5C31;&#x662F; html_str</span>
html_dict = json.loads(response.text)
data_list = html_dict[<span class="hljs-string">&apos;data&apos;</span>]
<span class="hljs-comment"># &#x901A;&#x8FC7;html_dict&#xFF0C; &#x80FD;&#x591F;&#x4EE5;&#x5B57;&#x5178;&#x7684;&#x65B9;&#x5F0F;&#x83B7;&#x53D6;&#x5230;image&#x7684;&#x5217;&#x8868;</span>
<span class="hljs-keyword">for</span> data <span class="hljs-keyword">in</span> data_list:
    <span class="hljs-comment"># &#x5224;&#x65AD;&#x662F;&#x5426;&#x5B58;&#x5728;image_list</span>
    <span class="hljs-keyword">if</span> <span class="hljs-string">&apos;image_list&apos;</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> data:
        <span class="hljs-keyword">continue</span>
    img_list = data[<span class="hljs-string">&apos;image_list&apos;</span>]
    <span class="hljs-comment"># &#x901A;&#x8FC7;image&#x7684;&#x5217;&#x8868;&#x83B7;&#x53D6;&#x5230;&#x6BCF;&#x4E00;&#x4E2A;image</span>
    <span class="hljs-keyword">for</span> img <span class="hljs-keyword">in</span> img_list:
        image_url = <span class="hljs-string">&apos;http:&apos;</span> + img[<span class="hljs-string">&apos;url&apos;</span>]
        <span class="hljs-comment"># &#x56FE;&#x7247;&#x7684;&#x6587;&#x4EF6;&#x540D;&#x9700;&#x8981;&#x6307;&#x5B9A;&#x4E0D;&#x4E00;&#x6837;&#x7684;&#x540D;&#x5B57;&#xFF0C;&#x6240;&#x4EE5;&#x6211;&#x4EEC;&#x53BB;&#x4E86;url&#x7684;&#x6700;&#x540E;&#x7684;&#x90A3;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x4EE5;/&#x5206;&#x5272;</span>
        filename = dictionary_image +<span class="hljs-string">&apos;/&apos;</span> +image_url.split(<span class="hljs-string">&apos;/&apos;</span>)[-<span class="hljs-number">1</span>] + <span class="hljs-string">&apos;.jpg&apos;</span>
        <span class="hljs-comment"># &#x9700;&#x8981;&#x4E0B;&#x8F7D;&#x56FE;&#x7247;&#xFF0C;&#x5C31;&#x7528;requests</span>
        image_res = requests.get(image_url)
        <span class="hljs-keyword">with</span> open(filename, <span class="hljs-string">&apos;wb&apos;</span>) <span class="hljs-keyword">as</span> f:
            f.write(image_res.content)
</code></pre>
<hr>
<h2 id="&#x5FAE;&#x535A;&#x7684;&#x722C;&#x53D6;&#xFF0C;&#x9700;&#x8981;&#x4FE1;&#x606F;&#x5165;&#x5E93;&#x3002;">&#x5FAE;&#x535A;&#x7684;&#x722C;&#x53D6;&#xFF0C;&#x9700;&#x8981;&#x4FE1;&#x606F;&#x5165;&#x5E93;&#x3002;</h2>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> requests
<span class="hljs-keyword">import</span> mysqlhelper
<span class="hljs-comment"># 1. &#x786E;&#x5B9A;&#x7684;&#x9700;&#x6C42;&#x662F; &#x67D0;&#x67D0;&#x67D0;&#x7684;&#x5FAE;&#x535A; &#x7684;&#x5FAE;&#x535A;&#x5185;&#x5BB9;&#x7684;&#x83B7;&#x53D6;</span>
<span class="hljs-comment"># 2. &#x627E;&#x5230;&#x4E86;&#x624B;&#x673A;&#x7AEF;&#x7684;&#x5185;&#x5BB9;&#xFF0C;&#x56E0;&#x4E3A;&#x7F51;&#x9875;&#x7AEF;&#x9700;&#x8981;&#x767B;&#x5F55;&#xFF0C; &#x592A;**</span>
base_url = <span class="hljs-string">&apos;https://m.weibo.cn/api/container/getIndex?uid=xxxxxxxxxx&amp;luicode=10000011&amp;lfid=100103type%3D1%26q%3D%E4%BD%95%E7%82%85&amp;type=uid&amp;value=xxxxxxxx&amp;containerid=107603xxxxxxxxx&amp;page={}&apos;</span>

<span class="hljs-comment"># helper = mysqlhelper.MysqlHelper()</span>
<span class="hljs-comment"># &#x4E0B;&#x8F7D;&#x6240;&#x6709;&#x7684;&#x8FD4;&#x56DE;&#x503C;</span>
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(<span class="hljs-number">0</span>, <span class="hljs-number">20</span>):
    url = base_url + <span class="hljs-string">&apos;&amp;page=&apos;</span> + str(i)
    response = requests.get(url)
    res_dict = response.json()
    <span class="hljs-comment"># &#x9700;&#x8981;&#x627E;&#x5230;&#x7684;&#x5185;&#x5BB9;&#x9996;&#x5148;&#x662F;data&#xFF0C; cards&#xFF0C;&#x7136;&#x540E;&#x5BF9;cards&#x5FAA;&#x73AF;&#xFF0C; &#x627E;&#x5230;mgblog&#x4E0B;&#x7684;created_at &#x548C; text</span>
    cards_list = res_dict[<span class="hljs-string">&apos;data&apos;</span>][<span class="hljs-string">&apos;cards&apos;</span>]
    print(<span class="hljs-string">&apos;download... page&apos;</span> + str(i))
    <span class="hljs-keyword">for</span> card <span class="hljs-keyword">in</span> cards_list:
        <span class="hljs-keyword">if</span> <span class="hljs-string">&apos;mblog&apos;</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> card:
            <span class="hljs-keyword">continue</span>
        created_at = card[<span class="hljs-string">&apos;mblog&apos;</span>][<span class="hljs-string">&apos;created_at&apos;</span>]
        text = card[<span class="hljs-string">&apos;mblog&apos;</span>][<span class="hljs-string">&apos;text&apos;</span>]
        print(created_at)
        print(text)
        <span class="hljs-comment"># &#x6211;&#x4EEC;&#x63D2;&#x5165;&#x7684;&#x65F6;&#x5019;&#x7684;insert&#x8BED;&#x53E5;</span>
        <span class="hljs-comment"># insert_sql  = &apos;insert into weibo(created_at, context) values(%s, %s)&apos;</span>
        <span class="hljs-comment"># &#x9700;&#x8981;&#x7528;&#x5230;&#x7684;&#x6570;&#x636E;&#x7684;&#x5185;&#x5BB9;</span>
        <span class="hljs-comment"># data = (created_at, text)</span>
        <span class="hljs-comment"># print(text)</span>
        <span class="hljs-comment"># &#x6267;&#x884C;&#x8FD9;&#x4E2A;insert&#x8BED;&#x53E5;&#xFF0C;&#x5C06;&#x6570;&#x636E;&#x5B58;&#x50A8;&#x5230;mysql&#x4E2D;</span>
        <span class="hljs-comment"># helper.execute_modify_sql(insert_sql, data)</span>

<span class="hljs-comment"># truncate &#x8868;&#x660E;------&#x6E05;&#x7A7A;&#x8868;</span>
</code></pre>
<hr>
<h2 id="&#x4FE1;&#x606F;&#x5165;&#x5E93;&#x9700;&#x8981;&#x8BB0;&#x4F4F;&#x4EE5;&#x53CA;&#x4E86;&#x89E3;&#x7684;&#x4EE3;&#x7801;">&#x4FE1;&#x606F;&#x5165;&#x5E93;&#x9700;&#x8981;&#x8BB0;&#x4F4F;&#x4EE5;&#x53CA;&#x4E86;&#x89E3;&#x7684;&#x4EE3;&#x7801;</h2>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> pymysql

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MysqlHelper</span><span class="hljs-params">(object)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self)</span>:</span>
    <span class="hljs-comment">#&#x6570;&#x636E;&#x5E93;&#x7684;&#x7528;&#x6237;&#x540D;&#xFF0C;&#x5BC6;&#x7801;&#xFF0C;&#x6570;&#x636E;&#x5E93;&#x7684;&#x5E93;&#x540D;&#xFF0C;&#x5B57;&#x7B26;&#x7F16;&#x7801;&#x7EA7;</span>
        self.conn = pymysql.connect(host=<span class="hljs-string">&apos;127.0.0.1&apos;</span>, port=<span class="hljs-number">3306</span>, user=<span class="hljs-string">&apos;root&apos;</span>, password=<span class="hljs-string">&apos;123456&apos;</span>, db=<span class="hljs-string">&apos;py12&apos;</span>, charset=<span class="hljs-string">&apos;utf8mb4&apos;</span>)
        self.cursor = self.conn.cursor()

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">execute_modify_sql</span><span class="hljs-params">(self, insert_sql, data)</span>:</span>
        self.cursor.execute(insert_sql, data)
        self.conn.commit()

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__del__</span><span class="hljs-params">(self)</span>:</span>
        self.cursor.close()
        self.conn.close()
<span class="hljs-comment">#&#x6D4B;&#x8BD5; &#x662F;&#x5426;&#x53EF;&#x4EE5;&#x63D2;&#x5165;&#x4FE1;&#x606F;&#x6210;&#x529F;</span>
<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
    insert_sql = <span class="hljs-string">&apos;insert into renxing (c1, c2) values(%s, %s)&apos;</span>
    data = (<span class="hljs-number">1</span>, <span class="hljs-string">&quot;&#x4E0D;&#x51C6;&#x4EFB;&#x6027;&apos;&quot;</span>)
    helper = MysqlHelper()
    helper.execute_modify_sql(insert_sql, data)
</code></pre>
<hr>
<h3 id="&#x6709;&#x9053;&#x7FFB;&#x8BD1;-&#x53EF;&#x4EE5;&#x505A;&#x4E00;&#x4E2A;&#x81EA;&#x5DF1;&#x7684;&#x82F1;&#x8BED;&#x5B57;&#x5178;--&#x54C8;&#x54C8;">&#x6709;&#x9053;&#x7FFB;&#x8BD1; &#x53EF;&#x4EE5;&#x505A;&#x4E00;&#x4E2A;&#x81EA;&#x5DF1;&#x7684;&#x82F1;&#x8BED;&#x5B57;&#x5178;  &#x54C8;&#x54C8;~~</h3>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> requests
<span class="hljs-keyword">import</span> time
<span class="hljs-keyword">import</span> random
<span class="hljs-keyword">import</span> hashlib

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">translate</span><span class="hljs-params">(trans_str)</span>:</span>
    <span class="hljs-comment"># &#x901A;&#x8FC7;&#x5206;&#x6790;javascript&#x7684;&#x4EE3;&#x7801;&#x53EF;&#x77E5;&#xFF0C; &#x6211;&#x4EEC;&#x9700;&#x8981;&#x627E;&#x5230;&#x7684;&#x662F;&#xFF1A;</span>
    <span class="hljs-comment"># salt = time + &#x968F;&#x673A;&#x6570;</span>
    salt = int(time.time()*<span class="hljs-number">1000</span>) + random.randint(<span class="hljs-number">0</span>, <span class="hljs-number">10</span>)
    salt = str(salt)
    sign = <span class="hljs-string">&quot;fanyideskweb&quot;</span> + trans_str + salt + <span class="hljs-string">&quot;6x(ZHw]mwzX#u0V7@yfwK&quot;</span>

    <span class="hljs-comment"># &#x83B7;&#x53D6;sign&#x7684;md5&#x7684;&#x503C;</span>
    md5_value = hashlib.md5()
    md5_value.update(sign.encode(<span class="hljs-string">&apos;utf-8&apos;</span>))
    sign = md5_value.hexdigest()

    <span class="hljs-comment"># sign</span>

    headers ={
        <span class="hljs-string">&apos;Cookie&apos;</span>: <span class="hljs-string">&apos;OUTFOX_SEARCH_USER_ID_NCOO=1891973180.1976018; _ga=GA1.2.1933331030.1534818606; OUTFOX_SEARCH_USER_ID=-42824455@10.169.0.84; JSESSIONID=aaa-Nwxc8TZrmg1pcQhxw; fanyi-ad-id=49843; fanyi-ad-closed=1; ___rl__test__cookies=1536653052506&apos;</span>,
        <span class="hljs-string">&apos;Referer&apos;</span>: <span class="hljs-string">&apos;http://fanyi.youdao.com/&apos;</span>,
        <span class="hljs-string">&apos;User-Agent&apos;</span>: <span class="hljs-string">&apos;Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36&apos;</span>
    }

    url = <span class="hljs-string">&apos;http://fanyi.youdao.com/translate_o?smartresult=dict&amp;smartresult=rule&apos;</span>
    form = {
        <span class="hljs-string">&apos;i&apos;</span>: trans_str,
        <span class="hljs-string">&apos;from&apos;</span>: <span class="hljs-string">&apos;AUTO&apos;</span>,
        <span class="hljs-string">&apos;to&apos;</span>: <span class="hljs-string">&apos;AUTO&apos;</span>,
        <span class="hljs-string">&apos;smartresult&apos;</span>: <span class="hljs-string">&apos;dict&apos;</span>,
        <span class="hljs-string">&apos;client&apos;</span>: <span class="hljs-string">&apos;fanyideskweb&apos;</span>,
        <span class="hljs-string">&apos;salt&apos;</span>: salt,
        <span class="hljs-string">&apos;sign&apos;</span>: sign,
        <span class="hljs-string">&apos;doctype&apos;</span>: <span class="hljs-string">&apos;json&apos;</span>,
        <span class="hljs-string">&apos;version&apos;</span>: <span class="hljs-string">&apos;2.1&apos;</span>,
        <span class="hljs-string">&apos;keyfrom&apos;</span>: <span class="hljs-string">&apos;fanyi.web&apos;</span>,
        <span class="hljs-string">&apos;action&apos;</span>: <span class="hljs-string">&apos;FY_BY_CLICKBUTTION&apos;</span>,
        <span class="hljs-string">&apos;typoResult&apos;</span>: <span class="hljs-string">&apos;false&apos;</span>,
    }

    response = requests.post(url, data=form, headers=headers)

    res_dict = response.json()
    translated_str = res_dict[<span class="hljs-string">&apos;translateResult&apos;</span>][<span class="hljs-number">0</span>][<span class="hljs-number">0</span>][<span class="hljs-string">&apos;tgt&apos;</span>]

    <span class="hljs-keyword">return</span> translated_str


<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
    <span class="hljs-keyword">while</span> <span class="hljs-keyword">True</span>:
        trans_str = input(<span class="hljs-string">&apos;&#x8BF7;&#x8F93;&#x5165;&#x6BD4;&#x8F83;&#x6709;&#x610F;&#x601D;&#x7684;&#x8BCD;&#x8BED;&#xFF1A;&apos;</span>)
        print(translate(trans_str))
</code></pre>
<hr>
<h3 id="&#x96EA;&#x7403;&#x7F51;&#x7684;&#x722C;&#x53D6;">&#x96EA;&#x7403;&#x7F51;&#x7684;&#x722C;&#x53D6;</h3>
<pre><code class="lang-python">
<span class="hljs-keyword">import</span> requests
<span class="hljs-keyword">import</span> json
<span class="hljs-keyword">import</span> mysqlhelper
headers = {
    <span class="hljs-string">&apos;Accept&apos;</span>:<span class="hljs-string">&apos;*/*&apos;</span>,
    <span class="hljs-comment"># &apos;Accept-Encoding&apos;:&apos;gzip, deflate, sdch, br&apos;,</span>
    <span class="hljs-string">&apos;Accept-Language&apos;</span>:<span class="hljs-string">&apos;zh-CN,zh;q=0.8&apos;</span>,
    <span class="hljs-string">&apos;Connection&apos;</span>:<span class="hljs-string">&apos;keep-alive&apos;</span>,
    <span class="hljs-string">&apos;Cookie&apos;</span>:<span class="hljs-string">&apos;aliyungf_tc=AQAAAKPgPEfvRgEAUhVFeQbSlA5+jFfh; xq_a_token=9c75d6bfbd0112c72b385fd95305e36563da53fb; xq_a_token.sig=-6-bcHntQlhRjsyrvsY2IGwh-B4; xq_r_token=9ad364aac7522791166c59720025e1f8f11bf9eb; xq_r_token.sig=usx1_hrblByw-9h0cXk1yLIUlL4; u=571536671880142; device_id=5b1260d03e2998f709ec489848aed7ba; _gat_gtag_UA_16079156_4=1; Hm_lvt_1db88642e346389874251b5a1eded6e3=1536671879,1536672134,1536672138; Hm_lpvt_1db88642e346389874251b5a1eded6e3=1536672138; _ga=GA1.2.100884861.1536671879; _gid=GA1.2.1528847784.1536671879&apos;</span>,
    <span class="hljs-string">&apos;Host&apos;</span>:<span class="hljs-string">&apos;xueqiu.com&apos;</span>,
    <span class="hljs-string">&apos;Referer&apos;</span>:<span class="hljs-string">&apos;https://xueqiu.com/&apos;</span>,
    <span class="hljs-string">&apos;User-Agent&apos;</span>:<span class="hljs-string">&apos;Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36&apos;</span>,
    <span class="hljs-string">&apos;X-Requested-With&apos;</span>:<span class="hljs-string">&apos;XMLHttpRequest&apos;</span>,
}
helper =mysqlhelper.MysqlHelper()
num = <span class="hljs-number">20307736</span>
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(<span class="hljs-number">10</span>):
    num -= <span class="hljs-number">15</span>
    url = <span class="hljs-string">&apos;https://xueqiu.com/v4/statuses/public_timeline_by_category.json?since_id=-1&amp;max_id=%s&amp;count=15&amp;category=-1&apos;</span>%(num)
    response = requests.get(url,headers=headers)
    <span class="hljs-comment"># if i &lt; 1:</span>
    <span class="hljs-comment">#     print(response.text)</span>

    html_dict = response.json()
    <span class="hljs-keyword">for</span> title_list <span class="hljs-keyword">in</span> html_dict[<span class="hljs-string">&apos;list&apos;</span>]:
        data = json.loads(title_list[<span class="hljs-string">&apos;data&apos;</span>])
        title = data[<span class="hljs-string">&apos;title&apos;</span>]
        description = data[<span class="hljs-string">&apos;description&apos;</span>]

        <span class="hljs-comment"># &#x6211;&#x4EEC;&#x63D2;&#x5165;&#x7684;&#x65F6;&#x5019;&#x7684;insert&#x8BED;&#x53E5;</span>
        insert_sql = <span class="hljs-string">&apos;insert into xueqiu(title,description) values(%s, %s)&apos;</span>
        <span class="hljs-comment"># &#x9700;&#x8981;&#x7528;&#x5230;&#x7684;&#x6570;&#x636E;&#x7684;&#x5185;&#x5BB9;</span>
        data = (title, description)
        <span class="hljs-comment"># &#x6267;&#x884C;&#x8FD9;&#x4E2A;insert&#x8BED;&#x53E5;&#xFF0C;&#x5C06;&#x6570;&#x636E;&#x5B58;&#x50A8;&#x5230;mysql&#x4E2D;</span>
        helper.execute_modify_sql(insert_sql, data)

        <span class="hljs-comment"># print(description)</span>
        <span class="hljs-comment"># print(&apos;----------&apos;)</span>
</code></pre>
<h3 id="&#x77E5;&#x4E4E;&#x7F51;&#x7684;&#x722C;&#x53D6;">&#x77E5;&#x4E4E;&#x7F51;&#x7684;&#x722C;&#x53D6;</h3>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> requests

headers={
    <span class="hljs-string">&apos;authority&apos;</span>:  <span class="hljs-string">&apos;www.zhihu.com&apos;</span>,
    <span class="hljs-string">&apos;method&apos;</span>:  <span class="hljs-string">&apos;GET&apos;</span>,
    <span class="hljs-comment"># &apos;:path&apos;:  &apos;/api/v4/search_v3?t=general&amp;q=%E7%88%AC%E8%99%AB&amp;correction=1&amp;offset=15&amp;limit=10&amp;search_hash_id=dc4fe7508a4a19cccb522f4647dcaceb&apos;,</span>
    <span class="hljs-string">&apos;scheme&apos;</span>:  <span class="hljs-string">&apos;https&apos;</span>,
    <span class="hljs-string">&apos;accept&apos;</span>:  <span class="hljs-string">&apos;*/*&apos;</span>,
    <span class="hljs-comment"># &apos;accept-encoding&apos;:  &apos;gzip, deflate, sdch, br&apos;,</span>
    <span class="hljs-string">&apos;accept-language&apos;</span>:  <span class="hljs-string">&apos;zh-CN,zh;q=0.8&apos;</span>,
    <span class="hljs-string">&apos;cookie&apos;</span>:<span class="hljs-string">&apos;tgw_l7_route=56f3b730f2eb8b75242a8095a22206f8; _zap=44e2e7dd-3191-49d0-8844-80193edea7f9; _xsrf=mYN0PN27KYiyMsjPt3nOx4hgihSg9voA; d_c0=&quot;ANBmZgZQMg6PTidZdVKx4vj_bYDFo-kyhfo=|1536674589&quot;; capsion_ticket=&quot;2|1:0|10:1536674791|14:capsion_ticket|44:ZjFhYjdlNWJkZGJmNDZiYzkyOWVkOTI5YzExYjg4ODU=|a474ebb5c5415c15e25569963bada7ac6cec2363df13d5fc6e1f3d8ac075615b&quot;; z_c0=&quot;2|1:0|10:1536674828|4:z_c0|92:Mi4xanhWY0NRQUFBQUFBMEdabUJsQXlEaVlBQUFCZ0FsVk5EQnFGWEFEUXh1eUFDTnJMbXhBeXlPSXZKMWVhYnF5d2VR|b4c5915e3bed3529fb47b0991083bd3d2f47f519c066dea70a503f29d738f724&quot;; unlock_ticket=&quot;AIDg-QdCkw0mAAAAYAJVTRTTl1tSK7R4Zdor7G3cvPZRv-EPW_4ZXg==&quot;; q_c1=b7e0ede5fa344f43a3c8027a2c78fadb|1536674828000|1536674828000&apos;</span>,
    <span class="hljs-string">&apos;referer&apos;</span>:  <span class="hljs-string">&apos;https://www.zhihu.com/search?type=content&amp;q=%E7%88%AC%E8%99%AB&apos;</span>,
    <span class="hljs-string">&apos;user-agent&apos;</span>:  <span class="hljs-string">&apos;Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36&apos;</span>,
}
num = <span class="hljs-number">5</span>
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(<span class="hljs-number">20</span>):
    num += <span class="hljs-number">10</span>
    url = <span class="hljs-string">&apos;https://www.zhihu.com/api/v4/search_v3?t=general&amp;q=%E7%88%AC%E8%99%AB&amp;correction=1&amp;offset=&apos;</span>+str(num)+<span class="hljs-string">&apos;&amp;limit=10&amp;search_hash_id=dc4fe7508a4a19cccb522f4647dcaceb&apos;</span>
    response = requests.get(url,headers=headers)
    html_dict = response.json()
    <span class="hljs-keyword">for</span> v <span class="hljs-keyword">in</span> html_dict[<span class="hljs-string">&apos;data&apos;</span>]:
        <span class="hljs-keyword">if</span> <span class="hljs-string">&apos;highlight&apos;</span> <span class="hljs-keyword">in</span> v:
            description =v[<span class="hljs-string">&apos;highlight&apos;</span>][<span class="hljs-string">&apos;description&apos;</span>]
            print(description)
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../认识爬虫/Requests.html" class="navigation navigation-prev " aria-label="Previous page: requests"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../正则表达式/正则表达式.html" class="navigation navigation-next " aria-label="Next page: 正则表达式"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
